package com.word.vacation.sdk.ad;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxReward;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.ads.MaxRewardedAd;
import com.word.vacation.sdk.WordConstant;


public class WordMaxReward implements WordVideoIAP {
    private static WordMaxReward adManager;
    private MaxRewardedAd maxRewardedAd;

    private WordVideoCallback mlistner;

    public static WordMaxReward getInstance() {
        if (adManager == null) {
            adManager = new WordMaxReward();
        }
        return adManager;
    }

    public void loadRewardAd(Context context) {
        maxRewardedAd =MaxRewardedAd.getInstance(WordConstant.REWARD_PLACEMENTID);
        mlistner=null;
        setListner();
        loadRewardAd();
    }

    private void loadRewardAd(){
        maxRewardedAd.loadAd();
        Log.e("WorV","load max");
    }


    private void setListner() {
        maxRewardedAd.setListener(new AdListener());
    }

    @Override
    public boolean isReady(){
        if (maxRewardedAd != null) {
            return maxRewardedAd.isReady();
        }
        return false;
    }

    @Override
    public void showVideo(Activity activity, WordVideoCallback listner) {
        if (maxRewardedAd != null) {
            mlistner=listner;
            setListner();
            show(activity);
        }else {
            onError();
        }

    }

    private void show(Activity activity) {
        if (isReady()) {
            maxRewardedAd.showAd(activity);
        } else {
            onError();
            loadRewardAd();
        }
    }

    public class AdListener implements MaxRewardedAdListener{

        @Override
        public void onUserRewarded(@NonNull MaxAd maxAd, @NonNull MaxReward maxReward) {

        }

        @Override
        public void onAdLoaded(@NonNull MaxAd maxAd) {
            Log.e("WorV","onAdLoaded");
        }

        @Override
        public void onAdDisplayed(@NonNull MaxAd maxAd) {
            Log.e("WorV","onAdDisplayed"+maxAd.getNetworkName());
        }

        @Override
        public void onAdHidden(@NonNull MaxAd maxAd) {
            loadRewardAd();
            if (mlistner != null) {
                mlistner.onAdOver();
            }
        }

        @Override
        public void onAdClicked(@NonNull MaxAd maxAd) {

        }

        @Override
        public void onAdLoadFailed(@NonNull String s, @NonNull MaxError error) {
            Log.d("WorV","onMaxRewardLoadFailed "+error.getCode()+ error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
        }

        @Override
        public void onAdDisplayFailed(@NonNull MaxAd maxAd, @NonNull MaxError error) {
            Log.e("WorV","onAdDisplayFailed "+error.getCode() +error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
            onError();
        }
    }

    private void onError(){
        if (mlistner != null) {
            mlistner.onAdError();
        }
    }
}
